<template>
  <div class="channelEdit">
    <!-- 我的频道区域 -->
    <van-cell title="我的频道" class="title" :border="false">
      <van-button plain hairline type="danger" size="mini" round class="edit" @click="isEdit = !isEdit">{{ isEdit ? '完成' : '编辑' }}</van-button>
    </van-cell>
    <van-grid :gutter="10" class="grid my-grid">
      <van-grid-item class="grid-btn" v-for="(channel, index) in channel" :key="index" @click="onMyChannelClick(channel, index)">
        <!-- 编辑按钮 -->
        <van-icon slot="icon" name="clear" v-show="isEdit && !fixeChannel.includes(channel.id)"></van-icon>
        <span slot="text" class="text" :class="{ active: index === active }">{{ channel.name }}</span>
      </van-grid-item>
    </van-grid>
    <!-- 频道推荐区域 -->
    <van-cell title="频道推荐" class="title commond" :border="false"> </van-cell>
    <van-grid :gutter="10" class="grid">
      <van-grid-item icon="plus" class="grid-btn" v-for="(item, index) in commend" :key="index" :text="item.name" @click="onAddchannel(item)" />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannelAPI, channelEditAPI, channelDeteleAPI } from '@/api/Channel.js'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage.js'
export default {
  name: 'channelEdit',
  props: {
    // 我的频道数据
    channel: {
      type: Array,
      require: true
    },
    active: {
      type: Number,
      require: true
    }
  },
  data() {
    return {
      allChannel: [], // 频道推荐数据
      isEdit: false, //控制编辑状态是否显示
      fixeChannel: [0] //固定频道，不允许删除
    }
  },
  created() {
    this.loadAllChannel()
  },
  methods: {
    async loadAllChannel() {
      try {
        const { data } = await getAllChannelAPI()
        // console.log(data)
        this.allChannel = data.data.channels
      } catch (err) {
        this.$toast('获取数据失败')
      }
    },
    // 添加操作
    async onAddchannel(item) {
      this.channel.push(item)
      // 数据持久化处理
      if (this.user) {
        try {
          // 已登录，把数据请求接口放到线上
          await channelEditAPI({
            id: item.id, //频道id
            seq: this.channel.length //顺序序号
          })
        } catch (err) {
          console.log(err)
        }
      } else {
        // 未登录，把数据存储到本地上
        setItem('TOUTIAO_ITEM', this.channel)
      }
    },
    // 删除操作
    onMyChannelClick(channel, index) {
      if (this.isEdit) {
        // 编辑状态下，执行删除频道
        // 当删除固定频道的时
        if (this.fixeChannel.includes(channel.id)) {
          return
        }
        // 当删除高亮之前的状态
        if (index <= this.active) {
          this.$emit('onChannel', this.active - 1, true)
        }
        this.channel.splice(index, 1)
        // 数据持久化，删除操作
        this.channelDetele(channel)
      } else {
        // 完成状态下，执行切换频道
        this.$emit('onChannel', index, false)
      }
    },
    // 删除频道，持久化
    async channelDetele(channel) {
      if (this.user) {
        try {
          // 已登录，则将数据更新到线上
          await channelDeteleAPI(channel.id)
        } catch (err) {
          console.log(err)
        }
      } else {
        // 未登录，将数据更新到本地
        setItem('TOUTIAO_ITEM', this.channel)
      }
    }
  },
  computed: {
    ...mapState(['user']),
    commend() {
      const channels = []
      this.allChannel.forEach(item => {
        const ret = this.channel.find(mychannel => {
          return mychannel.id === item.id
        })
        if (!ret) {
          channels.push(item)
        }
      })
      return channels
    }
  }
}
</script>

<style lang="less" scoped>
.channelEdit {
  padding: 85px 0;
  .title {
    font-size: 32px;
    color: #333333;
    .edit {
      width: 104px;
      height: 48px;
      font-size: 26px;
      color: #f85959;
      border: 1px solid #f85959;
    }
  }
  /deep/ .my-grid {
    .van-icon-clear {
      position: absolute;
      right: -10px;
      top: -10px;
      font-size: 30px;
      color: #a9a9a9;
      z-index: 3;
    }
  }
  .commond {
    margin-top: 70px;
  }

  /deep/ .grid {
    margin-top: 40px;
    .grid-btn {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          padding-right: 6px;
        }
        .van-grid-item__text {
          margin: 0;
        }
      }
      .text {
        font-size: 26px;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
    .van-grid-item__content {
      width: 160px;
      height: 86px;
      white-space: nowrap;
      background-color: #f4f5f6;
    }
  }

  .active {
    color: red;
  }
}
</style>
